<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>

<body>
    <script>
        var arr = [10, 23, 3, 42, 44, 22, 18];
        // var res = arr.filter(function(item) {
        //     return item < 30;
        // });
        // console.log(res);
        //自定义一个过滤方法
        Array.prototype.myFilter = function(callBack) {
            //this指向实例对象
            // console.log(this);
            //将筛选的值放入一个新的数组中
            var ret = [];
            //遍历
            for (var i = 0; i < this.length; i++) {
                var num = this[i];
                var flag = callBack(num);
                if (flag) {
                    ret.push(num);
                }
            }
            return ret;
        };
        var res = arr.myFilter(function(item) {
            return item < 15;
        });
        console.log(res);
    </script>
</body>

</html>